
package pos.control;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import pos.Pos;
import pos.entity.PriceAdjustment;
import pos.entity.Promotion;
import pos.storage.db.sql.SQL;

/**
 * The control object for the price adjustment entity
 * @author Justin Hume
 */
public class PriceAdjustmentControl {

    /**
     * Adds a new price adjustment to the repository
     * @param promo
     * @return a boolean indicating success or failure
     */
    public boolean add(PriceAdjustment adjustment) {
        try {
            //Set parameters in the prepared statement, in the following order:
            // PriceAdjustmentId
            // PriceAdjustmentAmount
            // PriceAdjustedByEmployeeId
            // PriceAdjustmentType
            // PriceAdjustmentDescription
            // PurchasedItemId
            // PromotionId

            //Obtain the prepared statement from the SQL list
            PreparedStatement priceAdjInsertStmt = Pos.adminConn.prepareStatement(SQL.INSERTPRICEADJUSTMENT);

            priceAdjInsertStmt.setLong(1, adjustment.getPriceAdjustmentId());
            priceAdjInsertStmt.setDouble(2, adjustment.getPriceAdjustmentAmount());
            priceAdjInsertStmt.setLong(3, adjustment.getAdjustedByEmployeeId());
            priceAdjInsertStmt.setInt(4, adjustment.getPriceAdjustmentType().ordinal());
            priceAdjInsertStmt.setString(5, adjustment.getPriceAdjustmentDescription());
            priceAdjInsertStmt.setLong(6, adjustment.getPurchasedItemId());
            priceAdjInsertStmt.setLong(7, adjustment.getPromotionId());

            priceAdjInsertStmt.executeUpdate();
            
            return true;
        }
        catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
}
